package com.jzx.controller;

import com.google.gson.Gson;
import com.jzx.bean.Information;
import com.jzx.service.InfoService;
import com.jzx.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

/**
 * @author JunedQQ
 * @create 2019/6/25
 * @since 1.0.0
 */
@Controller
public class InfoController {

    @Autowired
    private InfoService infoService;

    @Autowired
    private UserService userService;

    private Gson gson = new Gson();

    public static final String SERVER_ERROR = "服务器错误";
    public static final String SERVER_SUCCESS = "success";
    public static final String SERVER_FAILURE = "failure";
    public static final String CHARACTER_ENCODING = "UTF-8";
    public static final String NULL = "";

    @RequestMapping(path = "/getAllInfo")
    public void getAllInfo(HttpServletResponse response){
        //设置字符编码
        response.setCharacterEncoding(CHARACTER_ENCODING);
        //创建输出对象
        PrintWriter pw = null;
        try {
            //获取输出对象
            pw = response.getWriter();
            //查询所有案例
            List<Information> infos = infoService.getAllInfo();
            //转化成json对象
            String result = gson.toJson(infos);
            //传输数据
            pw.write(result);
        } catch (Exception e) {
            pw.write(SERVER_ERROR);
            e.printStackTrace();
        }finally {
            pw.close();
        }
    }

    @RequestMapping(path = "/getInfoById")
    public void getInfoById(HttpServletResponse response,String id){
        //设置字符编码
        response.setCharacterEncoding(CHARACTER_ENCODING);
        //创建输出对象
        PrintWriter pw = null;
        try {
            //获取输出对象
            pw = response.getWriter();
            //根据id来获取资讯
            Information info = infoService.getInfoById(id);
            //转化成json对象
            String result = gson.toJson(info);
            //传输数据
            pw.write(result);
        } catch (Exception e) {
            pw.write(SERVER_ERROR);
            e.printStackTrace();
        }finally {
            pw.close();
        }
    }

    @RequestMapping(path = "/getInfoByUserId")
    public void getInfoByUserId(HttpServletResponse response,String userId){
        //设置字符编码
        response.setCharacterEncoding(CHARACTER_ENCODING);
        //创建输出对象
        PrintWriter pw = null;
        try {
            //获取输出对象
            pw = response.getWriter();
            //查询用户id所有的资讯
           List<Information>  infos = infoService.getInfoByUserId(userId);
            //转化成json对象
            String result = gson.toJson(infos);
            //传输数据
            pw.write(result);
        } catch (Exception e) {
            pw.write(SERVER_ERROR);
            e.printStackTrace();
        }finally {
            pw.close();
        }
    }

    @RequestMapping(path = "/getInfoSeeById")
    public void getInfoSeeById(HttpServletResponse response,String id){
        //设置字符编码
        response.setCharacterEncoding(CHARACTER_ENCODING);
        //创建输出对象
        PrintWriter pw = null;
        try {
            //获取输出对象
            pw = response.getWriter();
            //查询id所对应的资讯
            int result = infoService.getInfoSeeById(id);
            //传输数据
            pw.write(result);
        } catch (Exception e) {
            pw.write(SERVER_ERROR);
            e.printStackTrace();
        }finally {
            pw.close();
        }
    }

    @RequestMapping(path = "/getInfoTitleById")
    public void getInfoTitleById(HttpServletResponse response,String id){
        //设置字符编码
        response.setCharacterEncoding(CHARACTER_ENCODING);
        //创建输出对象
        PrintWriter pw = null;
        try {
            //获取输出对象
            pw = response.getWriter();
            //获取案例的标题
            String title = infoService.getInfoTitleById(id);
            //传输数据
            pw.write(title);
        } catch (Exception e) {
            pw.write(SERVER_ERROR);
            e.printStackTrace();
        }finally {
            pw.close();
        }
    }

    @RequestMapping(path = "/getInfoPhotoAddressById")
    public void getInfoPhotoAddressById(HttpServletResponse response,String id){
        //设置字符编码
        response.setCharacterEncoding(CHARACTER_ENCODING);
        //创建输出对象
        PrintWriter pw = null;
        try {
            //获取输出对象
            pw = response.getWriter();
            //获取案例对应的图片地址
            List<String> photos = infoService.getInfoPhotoAddressById(id);
            //转化为json对象
            String photosJson = gson.toJson(photos);
            //传输数据
            pw.write(photosJson);
        } catch (Exception e) {
            pw.write(SERVER_ERROR);
            e.printStackTrace();
        }finally {
            pw.close();
        }
    }

    @RequestMapping(path = "/addInformation")
    public void addInformation(HttpServletResponse response,Information info){
        //设置字符编码
        response.setCharacterEncoding(CHARACTER_ENCODING);
        //创建输出对象
        PrintWriter pw = null;
        try {
            //获取输出对象
            pw = response.getWriter();
            //添加案例
            int result = infoService.addInformation(info);
            if(result == 1){
                //返回结果
                pw.write(SERVER_SUCCESS);
            }else{
                //返回结果
                pw.write(SERVER_FAILURE);
            }

        } catch (Exception e) {
            pw.write(SERVER_ERROR);
            e.printStackTrace();
        }finally {
            pw.close();
        }
    }

    @RequestMapping(path = "/deleteInfoById")
    public void deleteInfoById(HttpServletResponse response,String id){
        //设置字符编码
        response.setCharacterEncoding(CHARACTER_ENCODING);
        //创建输出对象
        PrintWriter pw = null;
        try {
            //获取输出对象
            pw = response.getWriter();
            //删除id所对应的的资讯
            int result = infoService.deleteInfoById(id);
            if(result == 1){
                //返回结果
                pw.write(SERVER_SUCCESS);
            }else{
                //返回结果
                pw.write(SERVER_FAILURE);
            }

        } catch (Exception e) {
            pw.write(SERVER_ERROR);
            e.printStackTrace();
        }finally {
            pw.close();
        }
    }

    @RequestMapping("/deleteInfoByUserId")
    public void deleteInfoByUserId(HttpServletResponse response,String userId){
        //设置字符编码
        response.setCharacterEncoding(CHARACTER_ENCODING);
        //创建输出对象
        PrintWriter pw = null;
        try {
            //获取输出对象
            pw = response.getWriter();
            //删除用户的所有资讯
            int result = infoService.deleteInfoByUserId(userId);
            if(result >= 1){
                //返回结果
                pw.write(SERVER_SUCCESS);
            }else{
                //返回结果
                pw.write(SERVER_FAILURE);
            }

        } catch (Exception e) {
            pw.write(SERVER_ERROR);
            e.printStackTrace();
        }finally {
            pw.close();
        }
    }

    @RequestMapping("/updateInfoUserIdById")
    public void updateInfoUserIdById(HttpServletResponse response,String id,String userId){
        //设置字符编码
        response.setCharacterEncoding(CHARACTER_ENCODING);
        //创建输出对象
        PrintWriter pw = null;
        try {
            //获取输出对象
            pw = response.getWriter();
            //更改id所对应的的userid
            int result = infoService.updateInfoUserIdById(id,userId);
            if(result == 1){
                //返回结果
                pw.write(SERVER_SUCCESS);
            }else{
                //返回结果
                pw.write(SERVER_FAILURE);
            }
        } catch (Exception e) {
            pw.write(SERVER_ERROR);
            e.printStackTrace();
        }finally {
            pw.close();
        }
    }

    @RequestMapping(path = "/infoSeePlusOne")
    public void infoSeePlusOne(HttpServletResponse response,String id){
        //设置字符编码
        response.setCharacterEncoding(CHARACTER_ENCODING);
        //创建输出对象
        PrintWriter pw = null;
        try {
            //获取输出对象
            pw = response.getWriter();
            //查看加一
            int result = infoService.infoSeePlusOne(id);
        } catch (Exception e) {
            pw.write(SERVER_ERROR);
            e.printStackTrace();
        }finally {
            pw.close();
        }
    }

    @RequestMapping(path = "/updateInfoTitleById")
    public void updateInfoTitleById(HttpServletResponse response,String id,String newTitle){
        //设置字符编码
        response.setCharacterEncoding(CHARACTER_ENCODING);
        //创建输出对象
        PrintWriter pw = null;
        try {
            //获取输出对象
            pw = response.getWriter();
            //更新标题
            int result = infoService.updateInfoTitleById(id,newTitle);
            if(result == 1){
                //返回结果
                pw.write(SERVER_SUCCESS);
            }else{
                //返回结果
                pw.write(SERVER_FAILURE);
            }
        } catch (Exception e) {
            pw.write(SERVER_ERROR);
            e.printStackTrace();
        }finally {
            pw.close();
        }
    }

    @RequestMapping(path = "/updateInfoPhotoAddressById")
    public void updateInfoPhotoAddressById(HttpServletResponse response,String id,String address){
        //设置字符编码
        response.setCharacterEncoding(CHARACTER_ENCODING);
        //创建输出对象
        PrintWriter pw = null;
        try {
            //获取输出对象
            pw = response.getWriter();
            //更新标题
            int result = infoService.updateInfoPhotoAddressById(id,address);
            if(result == 1){
                //返回结果
                pw.write(SERVER_SUCCESS);
            }else{
                //返回结果
                pw.write(SERVER_FAILURE);
            }
        } catch (Exception e) {
            pw.write(SERVER_ERROR);
            e.printStackTrace();
        }finally {
            pw.close();
        }
    }

    @RequestMapping(path = "/getTenInfo")
    public void getTenInfo(HttpServletResponse response,String start){
        //设置字符编码
        response.setCharacterEncoding(CHARACTER_ENCODING);
        //创建输出对象
        PrintWriter pw = null;
        try {
            //获取输出对象
            pw = response.getWriter();
            //更新标题
            List<Information> infos = infoService.getTenInfo(Integer.parseInt(start));
            //转化为json对象
            String infosJson = gson.toJson(infos);
            //输出数据
            pw.write(infosJson);
        } catch (Exception e) {
            pw.write(SERVER_ERROR);
            e.printStackTrace();
        }finally {
            pw.close();
        }
    }

    @RequestMapping(path = "/getAnyInfo")
    public void  getAnyInfo(HttpServletResponse response,String start,String count){
        //设置字符编码
        response.setCharacterEncoding(CHARACTER_ENCODING);
        //创建输出对象
        PrintWriter pw = null;
        try {
            //获取输出对象
            pw = response.getWriter();
            //更新标题
            List<Information> infos = infoService.getAnyInfo(Integer.parseInt(start),Integer.parseInt(count));
            //转化为json对象
            String infosJson = gson.toJson(infos);
            //输出数据
            pw.write(infosJson);
        } catch (Exception e) {
            pw.write(SERVER_ERROR);
            e.printStackTrace();
        }finally {
            pw.close();
        }
    }

}